﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ChartsDemo
{

    using System.Web.UI.DataVisualization.Charting;
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var x = default(Color);

            Chart chart1 = new Chart();

            chart1.Width = 600;
            chart1.Height = 500;

            ChartArea chartArea1 = new ChartArea("图表区1");
            chartArea1.AxisX.LineColor = Color.Blue;
            chartArea1.AxisX.InterlacedColor = Color.Red;

            chartArea1.AxisX.MajorGrid.LineColor = Color.White;

            chartArea1.AxisY.MajorGrid.LineColor = Color.White;
            chart1.ChartAreas.Add(chartArea1);


            Legend legend1 = new Legend("说明");
            chart1.Legends.Add(legend1);
            chart1.Legends[0].Enabled = true;

            Series series1 = new Series("数据1");
            chart1.Series.Add(series1);
            Series series2 = new Series("数据2");
            chart1.Series.Add(series2);
            //数据
            double[] yval = { 20, 60, 40, 50, 30, 30, 40, 20 };
            string[] xval = { "项目1", "项目2", "项目3", "项目4", "项目5", "项目6", "项目7", "项目8" };
            chart1.Series[0].Points.DataBindXY(xval, yval);
            double[] yval1 = { 201, 601, 401, 501, 30, 301, 410, 210 };
            string[] xval1 = { "项目1", "项目2", "项目3", "项目4", "项目5", "项目6", "项目7", "项目8" };
            chart1.Series[1].Points.DataBindXY(xval1, yval1);
            //是否启用3D显示
            chart1.ChartAreas[0].Area3DStyle.Enable3D = false;

            //显示类型,可以是柱形(默认Column) 折线Line,Spline 饼图Pie 条形图Bar 等等
            //  chart1.Series[0].ChartType = SeriesChartType.StackedColumn  ;

            //// Draw as 3D Cylinder
            chart1.Series[0]["DrawingStyle"] = "Cylinder";

            //像素点见宽度
            chart1.Series[0]["PointWidth"] = "0.8";

            //是否显示数值
            //  chart1.Series[0].IsValueShownAsLabel = true;

            //X轴数据显示间隔
            chart1.ChartAreas[0].AxisX.Interval = 1;

            //直角坐标显示,
            chart1.ChartAreas[0].Area3DStyle.IsRightAngleAxes = false;

            //是否群集在一起
            chart1.ChartAreas[0].Area3DStyle.IsClustered = false;

            //转动X轴角度
            chart1.ChartAreas[0].Area3DStyle.Inclination = 30;

            //转动Y轴角度
            chart1.ChartAreas[0].Area3DStyle.Rotation = 10;

            foreach (Series ser in chart1.Series)
            {
                ser.ChartType = SeriesChartType.Line;
                //DataPointCustomProperties d = new ;
                ser.IsValueShownAsLabel = true;
                //柱形宽度
                ser["PixelPointWidth"] = "40";
                //像素点深度
                ser["PixelPointDepth"] = "80";
                //像素点间隙深度
                ser["PixelPointGapDepth"] = "10";
            }
            chart1.SaveImage("1.png", ChartImageFormat.Bmp);
            MessageBox.Show("ok");

        }

        private void button2_Click(object sender, EventArgs e)
        {



            ChartHelper ch = new ChartHelper(700, 700, "非展厅销售线索");
            var area = new ChartArea();
            //清空坐标区域的单元格
            area.AxisX.MajorGrid.Enabled = false;
            area.AxisY.MajorGrid.Enabled = false;
            area.Name = "非展厅销售线索";

            ch.ChartArea = area;
            //说明
            ch.Legend = new Legend("非展厅销售线索");
            //数据

            double[] y1 = { 201, 601, 401, 501, 30, 301, 410, 210 };
            string[] x1 = { "项目1", "项目2", "项目3", "项目4", "项目5", "项目6", "项目7", "项目8" };
            var s1 = new Series("X5");
            s1.ChartType = SeriesChartType.StackedColumn;
            s1.Points.DataBindXY(x1, y1);
            var s2 = new Series("X6");
            s2.ChartType = SeriesChartType.StackedColumn;
            s2.Points.DataBindXY(x1, y1);
            var s3 = new Series("X8");
            s3.ChartType = SeriesChartType.StackedColumn;
            s3.Points.DataBindXY(x1, y1);
            var series = new List<Series>()
            {
                s1,s2,s3
            };
            ch.Series = series;
            ch.InitConfig();
            var chart = ch.GetChartInstance();
            //    chart.Titles.Add("非展厅销售线索");
            chart.SaveImage("2.png", ChartImageFormat.Png);



        }

        private void button3_Click(object sender, EventArgs e)
        {
            ChartHelper ch = new ChartHelper(700, 700, "邀约到店率");
            var area = new ChartArea();
            //清空坐标区域的单元格
            area.AxisX.MajorGrid.Enabled = false;
            area.AxisY.MajorGrid.Enabled = false;
            ch.ChartArea = area;
            //说明
            ch.Legend = new Legend("邀约到店率");
            //数据

            double[] y1 = { 201, 601, 401, 501, 30, 301, 410, 210, 200, 190, 34, 23 };
            string[] x1 = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
            var s1 = new Series("X5");
            s1.ChartType = SeriesChartType.Line;
            s1.Points.DataBindXY(x1, y1);
            //var s2 = new Series("X6");
            //s2.ChartType = SeriesChartType.StackedColumn;
            //s2.Points.DataBindXY(x1, y1);
            //var s3 = new Series("X8");
            //s3.ChartType = SeriesChartType.StackedColumn;
            //s3.Points.DataBindXY(x1, y1);
            var series = new List<Series>()
            {
                s1
            };
            ch.Series = series;
            ch.InitConfig();
            var chart = ch.GetChartInstance();

            chart.SaveImage("3.png", ChartImageFormat.Png);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ChartHelper ch = new ChartHelper(700, 700, "试驾率");
            var area = new ChartArea();
            //清空坐标区域的单元格
            area.AxisX.MajorGrid.Enabled = false;
            area.AxisY.MajorGrid.Enabled = false;
            ch.ChartArea = area;
            //说明
            ch.Legend = new Legend("试驾率");
            //数据

            double[] y1 = { 201, 601, 401, 501, 30, 301, 410, 210, 200, 190, 400, 210 };
            string[] x1 = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
            var s1 = new Series("X5");
            s1.ChartType = SeriesChartType.Line;
            s1.Points.DataBindXY(x1, y1);
            //var s2 = new Series("X6");
            //s2.ChartType = SeriesChartType.StackedColumn;
            //s2.Points.DataBindXY(x1, y1);
            //var s3 = new Series("X8");
            //s3.ChartType = SeriesChartType.StackedColumn;
            //s3.Points.DataBindXY(x1, y1);
            var series = new List<Series>()
            {
                s1
            };
            ch.Series = series;
            ch.InitConfig();
            var chart = ch.GetChartInstance();

            chart.SaveImage("4.png", ChartImageFormat.Png);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            ChartHelper ch = new ChartHelper(700, 700, "转化率");
            var area = new ChartArea();
            //清空坐标区域的单元格
            area.AxisX.MajorGrid.Enabled = false;
            area.AxisY.MajorGrid.Enabled = false;
            area.AxisX.LineWidth = 2;
            area.AxisY.LineWidth = 2;

            ch.ChartArea = area;
            //说明
            ch.Legend = new Legend("转化率");
            //数据

            double[] y1 = { 201, 601, 401, 501, 30, 301, 410, 210, 200, 190, 400, 210 };
            string[] x1 = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };

            double[] y2 = { 101, 501, 301, 401, 120, 201, 110, 310, 100, 190, 300, 110 };
            string[] x2 = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };

            var s1 = new Series("X5");
            s1.ChartType = SeriesChartType.Line;
            s1.Points.DataBindXY(x1, y1);
            var s2 = new Series("X6");
            s2.ChartType = SeriesChartType.Line;
            s2.Points.DataBindXY(x2, y2);
            //var s3 = new Series("X8");
            //s3.ChartType = SeriesChartType.StackedColumn;
            //s3.Points.DataBindXY(x1, y1);
            var series = new List<Series>()
            {
                s1,s2
            };
            ch.Series = series;
            ch.InitConfig();
            var chart = ch.GetChartInstance();

            chart.SaveImage("5.png", ChartImageFormat.Png);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Bitmap b = new Bitmap(2000, 3000);

            int margin = 20;//图片间的距离

            int left = 300;//图表图片距坐左边距离
            int leftWidth = 100;//左侧矩形宽度 
            int leftHight = 0;//左侧矩形的高度
            int top = 50;//图整体距上面的距离

            var pen = new Pen(Brushes.Gray, 2);
            using (Graphics g = Graphics.FromImage(b))
            {
                g.Clear(Color.White);
               
               //中间图表
                for (int i = 2; i <6; i++)
                {
                    //先画框，框里画图片 
                    var img = Image.FromFile(i + ".png");
                    var h = margin + (i - 2)*(img.Height+2*margin);
                    var reg = new Rectangle(left, h+10,img .Width+10,img.Height+10);
                    g.DrawRectangle(pen ,reg);
                   
                   g.DrawImage(img, left+5, h+15);
                    leftHight += img.Height;
                }
                
                //画左边矩形
     
               g.DrawRectangle(pen,margin,top ,leftWidth, leftHight);
                //画右面竖线
                g.Save();

            }
            b.Save("6.png", ImageFormat.Png);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            ChartHelper ch = new ChartHelper(700, 700, "邀约到店率");
            var area = new ChartArea();
            //清空坐标区域的单元格
            area.AxisX.MajorGrid.Enabled = false;
            area.AxisY.MajorGrid.Enabled = false;
            ch.ChartArea = area;
            //说明
            ch.Legend = new Legend("邀约到店率");
            //数据

            double[] y1 = { 201, 601, 401, 501, 30, 301, 410, 210, 200, 190, 34, 23 };
            string[] x1 = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" };
            var s1 = new Series("X5");
            s1.ChartType = SeriesChartType.StepLine;
            s1.Points.DataBindXY(x1, y1);
            //var s2 = new Series("X6");
            //s2.ChartType = SeriesChartType.StackedColumn;
            //s2.Points.DataBindXY(x1, y1);
            //var s3 = new Series("X8");
            //s3.ChartType = SeriesChartType.StackedColumn;
            //s3.Points.DataBindXY(x1, y1);
            var series = new List<Series>()
            {
                s1
            };
            ch.Series = series;
            ch.InitConfig();
            var chart = ch.GetChartInstance();

            chart.SaveImage("7.png", ChartImageFormat.Png);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Bitmap b = new Bitmap(640, 1000);
            int WIDTH = b.Width;
            int lineMargin = 150;//梯形和竖线的距离
            int w = 20;//据左边距离
            int h = 20;//据上面距离
            int bigWidth = b.Width - 2 * w - lineMargin * 2;//梯形上底长度
            int smallWidth = 120;//梯形下底长度
            int height = b.Height;//梯形高度
            int circleNum = 10;
            int circleWidth = 115;

            //画10个圈
            int margin = 20;//间距
            //圆所在矩形的左上角坐标
            int cx = (b.Width - lineMargin * 2 - circleWidth) / 2;
            int circleHeight = (height - (circleNum + 1) * margin - h) / circleNum;
            List<string> data = new List<string>() { "12.5%", "12%", "1%", "12.50%", "300.50%", "12.5%", "12.5%", "12.5%", "12.5%", "12.5%" };
            //传百分比数据
            List<double> lineData = new List<double>() { 12.5, -12, 1, 12.50, 300.50, 12.5, 12.5, 12.5, 12.5, 12.5 };
            Point[] p = new Point[]//四个梯形的坐标点
            {
                new Point (w,h),
                 new Point (w+bigWidth ,h),
                new Point ((bigWidth -smallWidth)/2+w+smallWidth,h+height),
                  new Point ((bigWidth -smallWidth)/2+w,h+height ),

            };


            Pen[] pens = new Pen[] { new Pen(Brushes.Yellow, 2), new Pen(Brushes.Green, 2), new Pen(Brushes.Red, 2) };


            using (var g = Graphics.FromImage(b))
            {
                g.Clear(Color.White);
                //画梯形
                g.FillClosedCurve(Brushes.Gray, p, System.Drawing.Drawing2D.FillMode.Alternate, 0);
                //画竖线
                g.DrawLine(new Pen(Brushes.Gray, 2), p[1].X + margin  +lineMargin, p[1].Y, p[1].X + margin + lineMargin, p[2].Y);


                for (int i = 1; i <= circleNum; i++)
                {
                    var sh = h + margin + (i - 1) * circleHeight + (i - 1) * margin;
                    g.DrawEllipse(pens[i % 3], new RectangleF(cx, sh, circleWidth, circleHeight));
                    var l = data[i - 1].Length;
                    //圆心
                    var circleX = cx + circleWidth / 2;
                    var circleY = sh + circleHeight / 2;
                    g.DrawString(data[i - 1], new Font("微软雅黑", 12), Brushes.White, new Point(circleX - l * 10 / 2, circleY - 10));
                    //线上的数值
                    var lineX = p[1].X + margin +lineMargin;
                    var lineY = circleY;
                    //画矩形
                    var lineBrush = lineData[i - 1] > 0 ? Brushes.Green : Brushes.Red;
               
                    var rec = lineData[i - 1] > 0
                        ? new Rectangle(lineX, lineY,(int) lineData[i - 1], 20)
                        : new Rectangle(lineX +(int)lineData[i - 1] , lineY,-(int) lineData[i - 1], 20);
                    g.FillRectangle(lineBrush, rec);
                    //画百分比
                    var word= lineData[i - 1].ToString("0.0") + "%";
                    var po = lineData[i - 1] > 0
                      ? new Point(lineX, lineY+20)
                      : new Point(lineX -word .Length *10, lineY+20);
                    g.DrawString(word, new Font("微软雅黑", 12), lineBrush, po);
                }


                g.Save();
            }
            b.Save("8.jpeg", ImageFormat.Jpeg);
        }
    }
}
